﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;

using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.Framework.Server.Base;
using ZhimeiSoft.CharmManager.Server.DAL.Interface;


namespace ZhimeiSoft.CharmManager.Server.DAL
{
    public class ReservationDAL : BaseDal, IReservationDAL
    {
        /// <summary>
        /// 增加预约信息
        /// </summary>
        public void AddResveration(DBEntityReservation reservatioinInfo, DataSet dsReservationServiceItem)
        {

            DbCommand cmdInsert = DB.GetSqlStringCommand(Reservation_Const.SQL_INSERT_RESERVATION);
            DB.AddInParameter(cmdInsert, DBEntityReservation.FieldName.CUSTOMERID, DbType.Int32, reservatioinInfo.CUSTOMERID);
            DB.AddInParameter(cmdInsert, DBEntityReservation.FieldName.RESERVATIONTIME, DbType.DateTime, reservatioinInfo.RESERVATIONTIME);
            DB.AddInParameter(cmdInsert, DBEntityReservation.FieldName.RMEMO, DbType.String, reservatioinInfo.RMEMO);
            DB.AddInParameter(cmdInsert, DBEntityReservation.FieldName.RSTATE, DbType.String, reservatioinInfo.RSTATE);
            DB.AddInParameter(cmdInsert, DBEntityReservation.FieldName.RWORKENDTIME, DbType.DateTime, reservatioinInfo.RWORKENDTIME);
            DB.AddInParameter(cmdInsert, DBEntityReservation.FieldName.RWORKSTARTTIME, DbType.DateTime, reservatioinInfo.RWORKSTARTTIME);
            DB.ExecuteNonQuery(cmdInsert);
            if (dsReservationServiceItem != null)
            {

                int reservationID = GetReservationID(reservatioinInfo);
                reservatioinInfo.RESERVATIONID = reservationID;
                DataRow[] drRows = dsReservationServiceItem.Tables[0].Select("", "", DataViewRowState.CurrentRows);
                foreach (DataRow dr in drRows)
                {
                    dr[DBEntityReservationServiceItem.FieldName.RESERVATIONID] = reservationID;
                }
                this.UpdateDataSet(dsReservationServiceItem, "RESERVATIONSERVICEITEM",
                        DBEntityReservationServiceItem.FieldName.RESERVATIONID + "," + DBEntityReservationServiceItem.FieldName.CUSTOMERSERVICEITEMID);
            }
        }

        public void UpdateReservation(DBEntityReservation reservatioinInfo, DataSet dsReservationServiceItem)
        {
            if (GetReservationRecord(reservatioinInfo) > 0)
            {
                //存在记录直接更新
                DbCommand cmdUupdate = DB.GetSqlStringCommand(Reservation_Const.SQL_UPDATE_RESERVATION);
                DB.AddInParameter(cmdUupdate, DBEntityReservation.FieldName.CUSTOMERID, DbType.Int32, reservatioinInfo.CUSTOMERID);
                DB.AddInParameter(cmdUupdate, DBEntityReservation.FieldName.RESERVATIONTIME, DbType.DateTime, reservatioinInfo.RESERVATIONTIME);
                DB.AddInParameter(cmdUupdate, DBEntityReservation.FieldName.RMEMO, DbType.String, reservatioinInfo.RMEMO);
                DB.AddInParameter(cmdUupdate, DBEntityReservation.FieldName.RSTATE, DbType.String, reservatioinInfo.RSTATE);
                DB.AddInParameter(cmdUupdate, DBEntityReservation.FieldName.RWORKENDTIME, DbType.DateTime, reservatioinInfo.RWORKENDTIME);
                DB.AddInParameter(cmdUupdate, DBEntityReservation.FieldName.RWORKSTARTTIME, DbType.DateTime, reservatioinInfo.RWORKSTARTTIME);
                DB.AddInParameter(cmdUupdate, DBEntityReservation.FieldName.RESERVATIONID, DbType.Int32, reservatioinInfo.RESERVATIONID);
                DB.ExecuteNonQuery(cmdUupdate);
                if (dsReservationServiceItem != null)
                {
                    this.UpdateDataSet(dsReservationServiceItem, "RESERVATIONSERVICEITEM", 
                        DBEntityReservationServiceItem.FieldName.RESERVATIONID + "," + DBEntityReservationServiceItem.FieldName.CUSTOMERSERVICEITEMID);
                }
            }
            else
            {
                //不存在记录调用新增保存
                AddResveration(reservatioinInfo, dsReservationServiceItem);
            }
            
        }
        /// <summary>
        /// 保存预约调整
        /// </summary>
        /// <param name="reservationInfo"></param>
        public void UpdateAdjReservation(DBEntityReservation reservationInfo)
        {
            //进行时间调整
            DbCommand cmd = DB.GetSqlStringCommand(Reservation_Const.SQL_UPDATE_RESERVATION_RESERVATIONTIME);
            DB.AddInParameter(cmd, DBEntityReservation.FieldName.RESERVATIONTIME, DbType.DateTime, reservationInfo.RESERVATIONTIME);
            DB.AddInParameter(cmd, DBEntityReservation.FieldName.RWORKSTARTTIME, DbType.DateTime, reservationInfo.RWORKSTARTTIME);
            DB.AddInParameter(cmd, DBEntityReservation.FieldName.RWORKENDTIME, DbType.DateTime, reservationInfo.RWORKENDTIME);
            DB.AddInParameter(cmd, DBEntityReservation.FieldName.RMEMO, DbType.String, reservationInfo.RMEMO);
            DB.AddInParameter(cmd, DBEntityReservation.FieldName.RESERVATIONID, DbType.Int16, reservationInfo.RESERVATIONID);
            DB.ExecuteNonQuery(cmd);
        }
        /// <summary>
        /// 进行预约项目的合并
        /// </summary>
        /// <param name="adjReservation"></param>
        /// <param name="updateReservationID"></param>
        public void UpdateAdjReservationServiceItem(int sourceReservationID, int targetReservationID)
        {
            if (sourceReservationID == targetReservationID)
            {
                //无需调整自己
                return;
            }
            DbTransaction aTracation = (DbTransaction)GetConnection().BeginTransaction();
            try
            {
                DbCommand cmd = DB.GetSqlStringCommand(Reservation_Const.SQL_UPDATE_RESERVATIONSERVICEITEM_ADJ);
                DB.AddInParameter(cmd, "TRAGETRESERVATIONID", DbType.Int16, targetReservationID);
                DB.AddInParameter(cmd, "SOURCERESERVATIONID", DbType.Int16, sourceReservationID);
                DB.ExecuteNonQuery(cmd, aTracation);

                cmd = DB.GetSqlStringCommand(ToDoListDAL_Const.SQL_QUERY_RESVERATIONTODOLIST);
                DB.AddInParameter(cmd, DBEntityReservationToDoList.FieldName.RESERVATIONID, DbType.Int16, sourceReservationID);
                DataSet dsToDoListID = DB.ExecuteDataSet(cmd);

                if (dsToDoListID.Tables[0].Rows.Count > 0)
                {
                    //删除原先的预约提醒记录
                    cmd = DB.GetSqlStringCommand(ToDoListDAL_Const.SQL_DELETE_RESVERATIONTODOLIST);
                    DB.AddInParameter(cmd, DBEntityReservation.FieldName.RESERVATIONID, DbType.Int16, sourceReservationID);
                    DB.AddInParameter(cmd, DBEntityToDoList.FieldName.TODOLISTID, DbType.Int16,
                        Convert.ToInt16(dsToDoListID.Tables[0].Rows[0][DBEntityToDoList.FieldName.TODOLISTID]));
                    DB.ExecuteNonQuery(cmd, aTracation);

                    cmd = DB.GetSqlStringCommand(ToDoListDAL_Const.SQL_DELETE_TODOLIST);
                    DB.AddInParameter(cmd, DBEntityToDoList.FieldName.TODOLISTID, DbType.Int16,
                        Convert.ToInt16(dsToDoListID.Tables[0].Rows[0][DBEntityToDoList.FieldName.TODOLISTID]));
                    DB.ExecuteNonQuery(cmd, aTracation);
                }
                //无预约项目将主记录删除
                cmd = DB.GetSqlStringCommand(Reservation_Const.SQL_QUERY_RESERVATIONSERVICEITEM_FORRESERVATIONID);
                DB.AddInParameter(cmd, DBEntityReservation.FieldName.RESERVATIONID, DbType.Int16, sourceReservationID);
                DataSet dsReservationServiceItem = DB.ExecuteDataSet(cmd, aTracation);
                if (dsReservationServiceItem.Tables[0].Rows.Count == 0)
                {
                    cmd = DB.GetSqlStringCommand(Reservation_Const.SQL_DELETE_RESERVATION);
                    DB.AddInParameter(cmd, DBEntityReservation.FieldName.RESERVATIONID, DbType.Int16, sourceReservationID);
                    DB.ExecuteNonQuery(cmd, aTracation);
                }
                aTracation.Commit();
            }
            catch (Exception ex)
            {
                aTracation.Rollback();
                throw ex;
            }
        }
        /// <summary>
        /// 更新预约状态
        /// </summary>
        /// <param name="reservationID"></param>
        /// <param name="customServiceItemID"></param>
        /// <param name="state"></param>
        public void UpdateReservationCustomServiceItemState(int reservationID, int customServiceItemID, int employeeID, string state)
        {
            DbCommand cmd = DB.GetSqlStringCommand(Reservation_Const.SQL_UPDATE_RESERVATIONSERVICEITEM_STATE);
            DB.AddInParameter(cmd, DBEntityReservationServiceItem.FieldName.RESERVATIONSTATE, DbType.String, state);
            DB.AddInParameter(cmd, DBEntityReservationServiceItem.FieldName.EMPLOYEEID, DbType.Int16, employeeID);
            DB.AddInParameter(cmd, DBEntityReservationServiceItem.FieldName.RESERVATIONID, DbType.Int16, reservationID);
            DB.AddInParameter(cmd, DBEntityReservationServiceItem.FieldName.CUSTOMERSERVICEITEMID, DbType.Int16, customServiceItemID);
            DB.ExecuteNonQuery(cmd);
        }

        public void UpdateReservationEmployee(int reservationID, int employeeID)
        {
            DbCommand cmd = DB.GetSqlStringCommand(Reservation_Const.SQL_UPDATE_RESERVATIONSERVICEITEM_EMPLOYEEID);
            DB.AddInParameter(cmd, DBEntityReservationServiceItem.FieldName.RESERVATIONID, DbType.Int16, reservationID);
            DB.AddInParameter(cmd, DBEntityReservationServiceItem.FieldName.EMPLOYEEID, DbType.Int16, employeeID);
            DB.ExecuteNonQuery(cmd);
        }

        /// <summary>
        /// 更新预约明细数据
        /// </summary>
        /// <param name="dsReservationServiceItem"></param>
        public void DeleteAdjReservationServiceItemDetail(DataSet dsReservationServiceItem)
        {
            if (dsReservationServiceItem != null)
            {
                DbTransaction aTracation = (DbTransaction)GetConnection().BeginTransaction();
                DbCommand cmd = null;
                try
                {
                    DataSet dsDel = dsReservationServiceItem.GetChanges(DataRowState.Deleted);
                    dsDel.RejectChanges();
                    foreach (DataRow dr in dsDel.Tables[0].Rows)
                    {
                        cmd = DB.GetSqlStringCommand(Reservation_Const.SQL_DELETE_RESERVATIONSERVICEITEM_DETAIL);
                        DB.AddInParameter(cmd, DBEntityReservationServiceItem.FieldName.RESERVATIONID, DbType.Int16, 
                            Convert.ToInt16(dr[DBEntityReservationServiceItem.FieldName.RESERVATIONID]));
                        DB.AddInParameter(cmd, DBEntityReservationServiceItem.FieldName.CUSTOMERSERVICEITEMID, DbType.Int16,
                            Convert.ToInt16(dr[DBEntityReservationServiceItem.FieldName.CUSTOMERSERVICEITEMID]));
                        DB.ExecuteNonQuery(cmd, aTracation);
                    }
                    aTracation.Commit();
                    dsReservationServiceItem.AcceptChanges();
                }
                catch (Exception ex)
                {
                    aTracation.Rollback();
                    throw ex;
                }
            }
        }
        public int GetReservationID(DBEntityReservation reservationInfo)
        {
            DbCommand cmdQuery = DB.GetSqlStringCommand(Reservation_Const.SQL_GET_RESERVATION);
            DB.AddInParameter(cmdQuery, DBEntityReservation.FieldName.CUSTOMERID, DbType.Int32, reservationInfo.CUSTOMERID);
            DB.AddInParameter(cmdQuery, "QUERYYEAR", DbType.Int32, reservationInfo.RESERVATIONTIME.Year);
            DB.AddInParameter(cmdQuery, "QUERYMONTH", DbType.Int32, reservationInfo.RESERVATIONTIME.Month);
            DB.AddInParameter(cmdQuery, "QUERYDAY", DbType.Int32, reservationInfo.RESERVATIONTIME.Day);
            DB.AddInParameter(cmdQuery, DBEntityReservation.FieldName.RSTATE, DbType.String, reservationInfo.RSTATE);
            DataSet ds = DB.ExecuteDataSet(cmdQuery);
            return Convert.ToInt32(ds.Tables[0].Rows[0][DBEntityReservation.FieldName.RESERVATIONID]);
        }

        private int GetReservationRecord(DBEntityReservation reservationInfo)
        {
            DbCommand cmdQuery = DB.GetSqlStringCommand(Reservation_Const.SQL_GET_RESERVATION);
            DB.AddInParameter(cmdQuery, DBEntityReservation.FieldName.CUSTOMERID, DbType.Int32, reservationInfo.CUSTOMERID);
            DB.AddInParameter(cmdQuery, "QUERYYEAR", DbType.Int32, reservationInfo.RESERVATIONTIME.Year);
            DB.AddInParameter(cmdQuery, "QUERYMONTH", DbType.Int32, reservationInfo.RESERVATIONTIME.Month); 
            DB.AddInParameter(cmdQuery, "QUERYDAY", DbType.Int32, reservationInfo.RESERVATIONTIME.Day);
            //DB.AddInParameter(cmdQuery, DBEntityReservation.FieldName.RSTATE, DbType.String, reservationInfo.RSTATE);
            DataSet ds = DB.ExecuteDataSet(cmdQuery);
            return ds.Tables[0].Rows.Count;
        }
        /// <summary>
        /// 删除预约项目
        /// </summary>
        /// <param name="reservationID">预约ID</param>
        public void DeleteReservation(int reservationID)
        {
            DbTransaction aTracation = (DbTransaction)GetConnection().BeginTransaction();
            try
            {
                DbCommand cmd = DB.GetSqlStringCommand(Reservation_Const.SQL_DELETE_TODOLIST);
                DB.AddInParameter(cmd, DBEntityReservation.FieldName.RESERVATIONID, DbType.Int32, reservationID);
                DB.ExecuteNonQuery(cmd, aTracation);

                cmd = DB.GetSqlStringCommand(Reservation_Const.SQL_DELETE_RESVERATIONTODOLIST);
                DB.AddInParameter(cmd, DBEntityReservation.FieldName.RESERVATIONID, DbType.Int32, reservationID);
                DB.ExecuteNonQuery(cmd, aTracation);

                cmd = DB.GetSqlStringCommand(Reservation_Const.SQL_DELETE_RESERVATIONSERVICEITEM);
                DB.AddInParameter(cmd, DBEntityReservation.FieldName.RESERVATIONID, DbType.Int32, reservationID);
                DB.ExecuteNonQuery(cmd, aTracation);

                cmd = DB.GetSqlStringCommand(Reservation_Const.SQL_DELETE_RESERVATION);
                DB.AddInParameter(cmd, DBEntityReservation.FieldName.RESERVATIONID, DbType.Int32, reservationID);
                DB.ExecuteNonQuery(cmd, aTracation);

                aTracation.Commit();
            }
            catch (Exception ex)
            {
                aTracation.Rollback();
                throw ex;
            }
        }

        public void StopResveration(DBEntityReservation reservatioinInfo)
        {
        }

        public void StopResverationForTime(DateTime currTime)
        {

        }
        /// <summary>
        /// 跟据月份查询预约信息
        /// </summary>
        /// <param name="queryYear">查询年</param>
        /// <param name="queryMonth">查询月</param>
        /// <returns></returns>
        public DataSet QueryReservationMonth(int queryYear, int queryMonth, string sfilter)
        {
            DbCommand cmdSelect = null;
            string sRunSQL = Reservation_Const.SQL_QUERY_RESERVATION_MONTH;
            if (queryYear == -1)
            {
                sRunSQL = Reservation_Const.SQL_QUERY_RESERVATION_MONTH_STATE;
                if (!sfilter.Equals(string.Empty))
                {
                    sRunSQL += " AND " + sfilter;
                }
                cmdSelect = DB.GetSqlStringCommand(sRunSQL);
                return DB.ExecuteDataSet(cmdSelect);
            }
            else
            {
                if (!sfilter.Equals(string.Empty))
                {
                    sRunSQL += " AND " + sfilter;
                }
                cmdSelect = DB.GetSqlStringCommand(sRunSQL);
            }
            DB.AddInParameter(cmdSelect, "QRYYEAR", DbType.Int16, queryYear);
            DB.AddInParameter(cmdSelect, "QRYMONTH", DbType.Int16, queryMonth);
            return DB.ExecuteDataSet(cmdSelect);
        }

        public DataSet QueryResveration(int queryYear, int queryMonth, int queryDay, string stateStr)
        {
            DbCommand cmdSelect;
            if (stateStr.Equals(string.Empty))
            {
                cmdSelect = DB.GetSqlStringCommand(Reservation_Const.SQL_QUERY_RESERVATION_CUSTOMINFO_TIME);
            }
            else
            {
                cmdSelect = DB.GetSqlStringCommand(Reservation_Const.SQL_QUERY_RESERVATION_CUSTOMINFO_TIME_STATE);
                DB.AddInParameter(cmdSelect, DBEntityReservation.FieldName.RSTATE, DbType.String, stateStr);
            }
            DB.AddInParameter(cmdSelect, "QUERYYEAR", DbType.Int32, queryYear);
            DB.AddInParameter(cmdSelect, "QUERYMONTH", DbType.Int32, queryMonth);
            DB.AddInParameter(cmdSelect, "QUERYDAY", DbType.Int32, queryDay);
            return DB.ExecuteDataSet(cmdSelect);
        }
        /// <summary>
        /// 获取员工预约信息
        /// </summary>
        /// <param name="queryYear">查询年</param>
        /// <param name="queryMonth">查询月</param>
        /// <param name="queryDay">查询日</param>
        /// <param name="employeeID">员工ID</param>
        /// <param name="stateStr">预约状态</param>
        /// <returns></returns>
        public DataSet QueryReservationEmployee(int queryYear, int queryMonth, int queryDay, int employeeID, string stateStr)
        {
            DbCommand cmdSelect = null;
            if (stateStr.Equals(string.Empty) && employeeID == -1)
            {
                //查询当天全部的预约记录
                cmdSelect = DB.GetSqlStringCommand(Reservation_Const.SQL_QUERY_RESERVATION_RESERVATIONSERVICEITEM_NULLEMPLOYEE);
            }
            else if (stateStr.Equals(string.Empty) && employeeID != -1)
            {
                //查询当天个人员工预约记录
                cmdSelect = DB.GetSqlStringCommand(Reservation_Const.SQL_QUERY_RESERVATION_RESERVATIONSERVICEITEM_EMPLOYEE);
                DB.AddInParameter(cmdSelect, DBEntityReservationServiceItem.FieldName.EMPLOYEEID, DbType.Int32, employeeID);
            }
            else if (!stateStr.Equals(string.Empty) && employeeID == -1)
            {
                //按照状态查询所有员工预约记录
                cmdSelect = DB.GetSqlStringCommand(Reservation_Const.SQL_QUERY_RESERVATION_NULLEMPLOYEE_RESERVATIONSERVICEITEM_STATE);
                DB.AddInParameter(cmdSelect, DBEntityReservation.FieldName.RSTATE, DbType.String, stateStr);
            }
            else if (!stateStr.Equals(string.Empty) && employeeID != -1)
            {
                //按照状态以及员工查询预约记录
                cmdSelect = DB.GetSqlStringCommand(Reservation_Const.SQL_QUERY_RESERVATION_RESERVATIONSERVICEITEM_CUSTOMINFO_STATE);
                DB.AddInParameter(cmdSelect, DBEntityReservationServiceItem.FieldName.EMPLOYEEID, DbType.Int32, employeeID);
                DB.AddInParameter(cmdSelect, DBEntityReservation.FieldName.RSTATE, DbType.String, stateStr);
            }
            
            DB.AddInParameter(cmdSelect, "QUERYYEAR", DbType.Int32, queryYear);
            DB.AddInParameter(cmdSelect, "QUERYMONTH", DbType.Int32, queryMonth);
            DB.AddInParameter(cmdSelect, "QUERYDAY", DbType.Int32, queryDay);
            return DB.ExecuteDataSet(cmdSelect);
        }
        /// <summary>
        /// 根据时间和会员查询预约
        /// </summary>
        /// <param name="querYear">年</param>
        /// <param name="queryMonth">月</param>
        /// <param name="queryDay">日</param>
        /// <param name="customID">会员ID</param>
        /// <param name="dsReservation">返回查询结果</param>
        /// <param name="error">错误信息</param>
        /// <returns></returns>
        public DataSet QueryReservationCustom(int queryYear, int queryMonth, int queryDay, int customID)
        {
            DbCommand cmdSelect = DB.GetSqlStringCommand(Reservation_Const.SQL_QUERY_RESERVATION_CUSTOM);
            DB.AddInParameter(cmdSelect, "QUERYYEAR", DbType.Int32, queryYear);
            DB.AddInParameter(cmdSelect, "QUERYMONTH", DbType.Int32, queryMonth);
            DB.AddInParameter(cmdSelect, "QUERYDAY", DbType.Int32, queryDay);
            DB.AddInParameter(cmdSelect, DBEntityCustomInfo.FieldName.CUSTOMERID, DbType.Int32, customID);
            DataSet dsResveration = DB.ExecuteDataSet(cmdSelect);

            cmdSelect = DB.GetSqlStringCommand(Reservation_Const.SQL_QUERY_RESERVATIONDETAIL_CUSTOM);
            DB.AddInParameter(cmdSelect, "QUERYYEAR", DbType.Int32, queryYear);
            DB.AddInParameter(cmdSelect, "QUERYMONTH", DbType.Int32, queryMonth);
            DB.AddInParameter(cmdSelect, "QUERYDAY", DbType.Int32, queryDay);
            DB.AddInParameter(cmdSelect, DBEntityCustomInfo.FieldName.CUSTOMERID, DbType.Int32, customID);
            DataSet dsReservationServiceItem = DB.ExecuteDataSet(cmdSelect);

            DataSet dsReturn = new DataSet();
            DataTable dtReservation = dsResveration.Tables[0].Copy();
            dtReservation.TableName = "RESERVATION";
            dsReturn.Tables.Add(dtReservation);

            DataTable dtReservationServiceItem = dsReservationServiceItem.Tables[0].Copy();
            dtReservationServiceItem.TableName = "RESERVATIONSERVICEITEM";
            dsReturn.Tables.Add(dtReservationServiceItem);

            return dsReturn;
        }
        /// <summary>
        /// 根据预约ID查询对应的服务项目
        /// </summary>
        /// <param name="reservationID"></param>
        /// <returns></returns>
        public DataSet QueryReservationServiceItem(int reservationID)
        {
            DbCommand cmdSelect = DB.GetSqlStringCommand(Reservation_Const.SQL_QUERY_RESERVATIONSERVICEITEM_SERVICEITEM);
            DB.AddInParameter(cmdSelect, DBEntityReservationServiceItem.FieldName.RESERVATIONID, DbType.Int32, reservationID);
            DataSet dsReturn = DB.ExecuteDataSet(cmdSelect);

            cmdSelect = DB.GetSqlStringCommand(Reservation_Const.SQL_QUERY_RESERVATION_ID);
            DB.AddInParameter(cmdSelect, DBEntityReservationServiceItem.FieldName.RESERVATIONID, DbType.Int32, reservationID);
            DataSet dsTmp = DB.ExecuteDataSet(cmdSelect);
            DataTable dtTmp = dsTmp.Tables[0].Copy();
            dtTmp.TableName = "DBEntityReservation";
            dsReturn.Tables.Add(dtTmp);
            return dsReturn;
        }
        /// <summary>
        /// 查询会员卡项预约情况
        /// </summary>
        /// <param name="customID">会员ID</param>
        /// <param name="customServiceItemID">会员服务项目ID</param>
        /// <returns></returns>
        public DataSet QeuryReservationCustomServiceItem(int customID, int customServiceItemID)
        {
            DbCommand cmdSelect = DB.GetSqlStringCommand(Reservation_Const.SQL_QUERY_RESERVATION_CUSTOMSERVICEITEM);
            DB.AddInParameter(cmdSelect, DBEntityReservation.FieldName.CUSTOMERID, DbType.Int16, customID);
            DB.AddInParameter(cmdSelect, DBEntityReservationServiceItem.FieldName.CUSTOMERSERVICEITEMID, DbType.Int16, customServiceItemID);
            return DB.ExecuteDataSet(cmdSelect);
        }
        /// <summary>
        /// 查询会员卡预约情况
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public DataSet QueryReservationCustomServiceItemFilter(string filter)
        {
            string sql = Reservation_Const.SQL_QUERY_RESERVATION_CUSTOMSERVICEITEM_FILTER  + ((filter == "")?"" : " AND ") + filter;
            DbCommand cmdSelect = DB.GetSqlStringCommand(sql);
            return DB.ExecuteDataSet(cmdSelect);
        }
        /// <summary>
        /// 根据预约ID查询详细项目
        /// </summary>
        /// <param name="reservationID">预约ＩＤ</param>
        /// <returns></returns>
        public DataSet QueryReservationServiceItemDetail(int reservationID){
            DbCommand cmdSelect = DB.GetSqlStringCommand(Reservation_Const.SQL_QUERY_RESERVATIONSERVICEITEM_RESERVATIONID);
            DB.AddInParameter(cmdSelect, DBEntityReservationServiceItem.FieldName.RESERVATIONID, DbType.Int16, reservationID);
            return DB.ExecuteDataSet(cmdSelect);
        }

        /// <summary>
        /// 更新过期的预约状态
        /// </summary>
        /// <param name="error">错误信息</param>
        /// <returns></returns>
        public void CheckAllReservationState()
        {
            DbCommand cmdExeSQL = DB.GetSqlStringCommand(Reservation_Const.SQL_UPDATE_RESERVATIONSTATE);
            DB.AddInParameter(cmdExeSQL, DBEntityReservation.FieldName.RSTATE, DbType.String, DBEntityReservation.ENUM_RSTATE.C0D);
            DB.AddInParameter(cmdExeSQL, DBEntityReservation.FieldName.RESERVATIONTIME, DbType.String, DateTime.Now.ToShortDateString());
            DB.ExecuteNonQuery(cmdExeSQL);
        }
        /// <summary>
        /// 根据预约ID更新预约状态
        /// </summary>
        /// <param name="reservationID"></param>
        /// <param name="reservationDateTime"></param>
        public void UpdateReservationState(int reservationID, DBEntityReservation reservation ,DateTime dtReservationTime)
        {
            int reservationTime = dtReservationTime.Year * 10000 + dtReservationTime.Month * 100 + dtReservationTime.Day;
            int nowTime = DateTime.Now.Year * 10000 + DateTime.Now.Month * 100 + DateTime.Now.Day;
            string sState = string.Empty;
            if (reservationTime < nowTime)
            {
                //过期
                sState = DBEntityReservation.ENUM_RSTATE.C0D;
            }
            else if(reservationTime >= nowTime)
            {
                sState = DBEntityReservation.ENUM_RSTATE.C0N;
            }
            if (reservation.RSTATE == DBEntityReservation.ENUM_RSTATE.C0A)
            {
                sState = DBEntityReservation.ENUM_RSTATE.C0A;
            }
            //预约完成的不能够进行调整
            DbCommand cmdExeSQL = DB.GetSqlStringCommand(Reservation_Const.SQL_UPDATE_RESERVATIONSTATE_RESERVATIONID);
            DB.AddInParameter(cmdExeSQL, DBEntityReservation.FieldName.RSTATE, DbType.String, sState);
            DB.AddInParameter(cmdExeSQL, DBEntityReservation.FieldName.RESERVATIONID, DbType.Int16, reservationID);
            DB.ExecuteNonQuery(cmdExeSQL);
        }

        /// <summary>
        /// 预约时间
        /// </summary>
        /// <param name="reservation"></param>
        /// <returns></returns>
        public DataSet getEmpViewReservationData(DateTime reservation)
        {
            return null;
        }
    }
}
